package com.zmkm.controller.user;

import com.zmkm.common.Const;
import com.zmkm.common.ServerResponse;
import com.zmkm.entity.Housekeeper;
import com.zmkm.service.IUserService;

import java.util.ArrayList;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({ "/housekeeper_manage/" })
public class HousekeeperManage {
	@Autowired
	private IUserService iUserService;

	@RequestMapping(value = { "insert_housekeeper" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView insertHousekeeper(HttpServletRequest request) {
		ServerResponse response = (ServerResponse) GradeVerify(request, Const.ADMAX).getModel().get("serverResponse");
		int user_grade;
		if (response.getStatus() != 0) {
			int data = Integer.parseInt((String) request.getSession().getAttribute("insertGrade"));
			user_grade = data;
		}else{
			user_grade = Integer.parseInt(request.getParameter("user_grade"));
		}
		
		ModelAndView mv = new ModelAndView();
		String userName = request.getParameter("userName");
		String passWord = request.getParameter("passWord");
		String phone = request.getParameter("phone");
		String housekeeper_address = request.getParameter("housekeeper_address");
		String name = request.getParameter("name");
		String identity_card = request.getParameter("identity_card");
		String identity_address = request.getParameter("identity_address");
		Housekeeper housekeeper = new Housekeeper();
		housekeeper.serNewHousekeeper(userName, passWord, phone, housekeeper_address, name, identity_card,
				identity_address, Integer.valueOf(user_grade));
		ServerResponse insertResponse = this.iUserService.userInsert(housekeeper);
		mv.addObject("submitReturn", insertResponse);
		mv.addObject("returnWeb", "admin/housekeeper");
		mv.setViewName("admin_housekeeper/submitReturn");
		return mv;
	}

	@RequestMapping(value = { "selectByOneUser" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView selectByOneUser(HttpServletRequest request) {
		ServerResponse response = (ServerResponse) GradeVerify(request, Const.ADMAX).getModel().get("serverResponse");
		if (response.getStatus() != 0) {
			return GradeVerify(request, Const.ADMAX);
		}
		ModelAndView mv = new ModelAndView();
		String switchNumber = request.getParameter("select_type");
		String search = request.getParameter("select_input");
		ServerResponse userResponse = this.iUserService.selectByOneUser(switchNumber, search);

		request.setAttribute("userResponse", userResponse);
		mv.setViewName("admin_housekeeper/housekeeper_select");
		return mv;
	}

	@RequestMapping(value = { "update_housekeeper" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView updateHousekeeper(HttpServletRequest request) {
		ServerResponse response = (ServerResponse) GradeVerify(request, Const.ADMAX).getModel().get("serverResponse");
		if (response.getStatus() != 0) {
			return GradeVerify(request, Const.ADMAX);
		}
		ModelAndView mv = new ModelAndView();
		String userId = request.getParameter("userId");
		Housekeeper housekeeper = this.iUserService.selectById(Integer.valueOf(Integer.parseInt(userId)));
		mv.addObject("userSelect", housekeeper);
		mv.setViewName("admin_housekeeper/updateHousekeeper");
		return mv;
	}

	@RequestMapping(value = { "update_housekeeper_submit" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView updateHousekeeperSubmit(HttpServletRequest request) {
		ServerResponse response = (ServerResponse) GradeVerify(request, Const.ADMAX).getModel().get("serverResponse");
		if (response.getStatus() != 0) {
			return GradeVerify(request, Const.ADMAX);
		}
		ModelAndView mv = new ModelAndView();
		Integer id = Integer.valueOf(Integer.parseInt(request.getParameter("userId")));
		String userName = request.getParameter("userName");
		String passWord = request.getParameter("passWord");
		String phone = request.getParameter("phone");
		String housekeeper_address = request.getParameter("housekeeper_address");
		String name = request.getParameter("name");
		String identity_card = request.getParameter("identity_card");
		String identity_address = request.getParameter("identity_address");
		int user_grade = Integer.parseInt(request.getParameter("user_grade"));
		Housekeeper housekeeper = new Housekeeper();
		housekeeper.serNewHousekeeper(userName, passWord, phone, housekeeper_address, name, identity_card,
				identity_address, Integer.valueOf(user_grade));
		housekeeper.setId(id);
		ServerResponse updateResponse = this.iUserService.updateById(housekeeper);
		mv.addObject("submitReturn", updateResponse);
		mv.addObject("returnWeb", "admin/housekeeper");
		mv.setViewName("admin_housekeeper/submitReturn");
		return mv;
	}

	@RequestMapping(value = { "select_one_housekeeper" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView select_one_housekeeper(HttpServletRequest request) {
		ServerResponse response = (ServerResponse) GradeVerify(request, Const.ADMAX).getModel().get("serverResponse");
		if (response.getStatus() != 0) {
			return GradeVerify(request, Const.ADMAX);
		}
		ModelAndView mv = new ModelAndView();
		String userId = request.getParameter("userId");
		Housekeeper housekeeper = this.iUserService.selectById(Integer.valueOf(Integer.parseInt(userId)));
		mv.addObject("userSelect", housekeeper);
		mv.setViewName("admin_housekeeper/one_housekeeper");
		return mv;
	}

	@RequestMapping(value = { "delete_housekeeper_submit" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView deleteHousekeeperSubmit(HttpServletRequest request) {
		ServerResponse response = (ServerResponse) GradeVerify(request, Const.ADMAX).getModel().get("serverResponse");
		if (response.getStatus() != 0) {
			return GradeVerify(request, Const.ADMAX);
		}
		ModelAndView mv = new ModelAndView();
		String userId = request.getParameter("userId");
		ServerResponse delectResponse = this.iUserService.delectById(Integer.valueOf(Integer.parseInt(userId)));
		mv.addObject("submitReturn", delectResponse);
		mv.addObject("returnWeb", "admin/housekeeper");
		mv.setViewName("admin_housekeeper/submitReturn");
		return mv;
	}

	public ModelAndView GradeVerify(HttpServletRequest request,ArrayList<Integer> grade) {
		ModelAndView mv = new ModelAndView();
		Housekeeper data = (Housekeeper) request.getSession().getAttribute("housekeeper");
		if (data == null) {
			mv.addObject("serverResponse", ServerResponse.createByErrorMessage("未登陆，请登陆后在进行操作！"));
			mv.setViewName("login");
			return mv;
		}
		if (data != null) {
			ServerResponse verify = this.iUserService.userNameAndpassWordVerify(data.getUserName(), data.getPassWord());
			if (verify.getStatus() == 1) {
				mv.addObject("serverResponse", verify);
				mv.setViewName("login");
				return mv;
			}
			if (!(grade.contains(data.getUser_grade()) || Const.ADMAX.contains(data.getUser_grade()))) {
				mv.addObject("serverResponse", ServerResponse.createByErrorMessage("没有这个操作权限!"));
				request.getSession().setAttribute("housekeeper", data);
				mv.setViewName("index");
				return mv;
			}
		}
		mv.addObject("serverResponse", ServerResponse.createBySuccess());
		return mv;
	}
}

